package model;

import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public class ProductService {
	private ProductDAO dao = new ProductDAO();

	public List<String[]> select(ProductBean bean) {
		List<String[]> result = null;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
		
		if (bean.getId() != 0) {
			ProductBean pbean = dao.select(bean.getId());
			if(pbean!=null){
				String[] pstring = new String[5];
				pstring[0] = "" + pbean.getId();
				pstring[1] = pbean.getName();
				pstring[2] = "" + pbean.getPrice();
				pstring[3] = pbean.getMake() == null ? null : sdf.format(pbean
						.getMake());
				pstring[4] = "" + pbean.getExpire();
				result = new ArrayList<String[]>();
				result.add(pstring);
			}
			
		
		} else if (bean.getName()!=null) {
			result = dao.select(bean.getName());
		} else {
			result = dao.select();

		}

		return result;
	}

	public ProductBean insert(ProductBean bean) {
		ProductBean result = null;
		if (bean.getName() == null || bean.getName().length() == 0) {
			result = dao.insert(bean.getId());
		} else {
			result = dao.insert(bean);
		}

		return result;
	}

	public ProductBean update(ProductBean bean) {
		return this.dao.update(bean);
	}

	public int delete(ProductBean bean) {
		return dao.delete(bean.getId());
	}
}
